class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    vector<bool> check;
    int target;
    void dfs(vector<int> &nums, int pos, int sum)
    {
        if(sum >= target)
        {
            if(sum == target)
                ret.push_back(path);
            return;
        }

        // 不能包含重复子集
        for(int i = pos; i < nums.size(); i++)
        {
            // 只关心"不合法"的分支
            // if (i > 0 && nums[i] == nums[i - 1] && check[i - 1] == false) {
            //     continue;
            // }

            // 只关心"合法"的分支
            if(check[i] == false && (i == 0 || nums[i] != nums[i - 1] || check[i - 1] == true))
            {
                check[i] = true;
                path.push_back(nums[i]);
                dfs(nums, i + 1, sum + nums[i]);
                path.pop_back();
                check[i] = false;
            }
        }
    }
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int _target) {
        // 排序，保证相同的元素都挨在一起，本题类似《全排列Ⅱ》
        sort(candidates.begin(), candidates.end());
        target = _target;
        check = vector<bool>(101);
        dfs(candidates, 0, 0);
        return ret;
    }
};